package com.hh.workorder.controller;

import com.hh.workorder.entity.RepairWorkOrder;
import com.hh.workorder.service.RepairWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/repair-work-orders")
public class RepairWorkOrderController {

    private final RepairWorkOrderService service;

    @Autowired
    public RepairWorkOrderController(RepairWorkOrderService service) {
        this.service = service;
    }

    @GetMapping
    public List<RepairWorkOrder> getAllRepairWorkOrders() {
        return service.getAllRepairWorkOrders();
    }

    @GetMapping("/search")
    public List<RepairWorkOrder> searchRepairWorkOrders(
            @RequestParam(required = false) String id_like,
            @RequestParam(required = false) String title_like,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String device_like,
            @RequestParam(required = false) String assignee,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return service.searchRepairWorkOrders(id_like, title_like, status, device_like, assignee, startDate, endDate);
    }
    @PostMapping
    public ResponseEntity<RepairWorkOrder> createWorkOrder(@RequestBody RepairWorkOrder workOrder) {
        workOrder.setCreateTime(LocalDateTime.now());
        workOrder.setUpdateTime(LocalDateTime.now());
        workOrder.setOrderStatus("待派发"); // 确保状态默认值
        RepairWorkOrder savedOrder = service.save(workOrder);
        return ResponseEntity.ok(savedOrder);
    }

}